package chattle.server;

import java.util.HashSet;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UserManager {
	
	private static final UserService userService = UserServiceFactory.getUserService();
	
	public static chattle.shared.User getCurrentUser(PersistenceManager pm)
	{
		com.google.appengine.api.users.User gaeUser = userService.getCurrentUser();
		if(gaeUser == null)
			return null;
		chattle.shared.User cUser;
		try
		{
			cUser = pm.getObjectById(chattle.shared.User.class, gaeUser.getUserId());
		}
		catch(JDOObjectNotFoundException e)
		{
			cUser = new chattle.shared.User(gaeUser.getUserId(), gaeUser.getNickname());
			pm.makePersistent(cUser);
		}
		return cUser;
	}
	
	public static String getLoginURL(String openIdUrl, String backURL) {
		return userService.createLoginURL(backURL, null, openIdUrl, new HashSet<String>());
	}
	public static String getLogoutURL(String backURL) {
		return userService.createLogoutURL(backURL, null);
	}
}
